{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_substring_indices(parent_string, substring):  \n",
    "    start_index = parent_string.find(substring)  \n",
    "    if start_index != -1:\n",
    "        end_index = start_index + len(substring)\n",
    "        return start_index, end_index  \n",
    "    else:  \n",
    "        return -1, -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "子字符串 '三大攻坚战' 在父字符串中的开始下标是 0，结束下标是 5\n"
     ]
    }
   ],
   "source": [
    "parent_string = \"三大攻坚战取得关键进展\"  \n",
    "substring = \"三大攻坚战\"  \n",
    "start_idx, end_idx = find_substring_indices(parent_string, substring)  \n",
    "  \n",
    "if start_idx != -1 and end_idx != -1:  \n",
    "    print(f\"子字符串 '{substring}' 在父字符串中的开始下标是 {start_idx}，结束下标是 {end_idx}\")  \n",
    "else:  \n",
    "    print(f\"子字符串 '{substring}' 没有在父字符串中找到\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# {\"id\":17168,\"text\":\"三大攻坚战取得关键进展\",\"label\":[[0,5,\"任务\"]],\"Comments\":[]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(\"三大攻坚战取得关键进展\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "def tran_llm_doccano(input_file, output_file, schema):\n",
    "    doccano_format = {\n",
    "        \"text\": None,\n",
    "        \"label\": [],\n",
    "        \"Comments\": []\n",
    "    }\n",
    "\n",
    "    def _find_text(text):\n",
    "        pattern = r'text:(.*?)\",'  \n",
    "        match = re.search(pattern, text, re.MULTILINE)\n",
    "        text_content = match.group(1)\n",
    "        return text_content\n",
    "\n",
    "    with open(input_file, 'r') as f:\n",
    "        with open(output_file, 'w') as w:\n",
    "            for line in f:\n",
    "                text = _find_text(line)\n",
    "                doccano_format[\"text\"] = text\n",
    "                data = json.loads(line)\n",
    "                predict = data[\"predict\"]\n",
    "                tmp = []\n",
    "                for ent_cls in schema:\n",
    "                    for predict_ent_name in predict[ent_cls]:\n",
    "                        start_idx, end_idx = find_substring_indices(text, predict_ent_name)\n",
    "                        if start_idx == -1 or end_idx == -1:\n",
    "                            continue\n",
    "                        tmp.append([start_idx, end_idx, ent_cls])\n",
    "                doccano_format[\"label\"] = tmp\n",
    "            w.write(json.dumps(doccano_format, ensure_ascii=False) + '\\n')\n",
    "\n",
    "schema = ['数据', '项目', '任务']\n",
    "tran_llm_doccano('llm_predict.jsonl', \"doccano_import.jsonl\", schema)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nlp",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
